﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.IO;

namespace TVC_WEB.Admin.Page
{
    public partial class LookupParams : System.Web.UI.Page
    {
        string sErr = "";
        string _db = "EXA";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request["lookupcate"] != null)
                txtID.Text = Request["lookupcate"].ToString();

        }
        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm("");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DTO.LookupsInfo ansInf = new DTO.LookupsInfo();
            ansInf = GetDataFromForm(ansInf);
            BUS.LookupsControl ctr = new BUS.LookupsControl();
            if (ctr.IsExist(txtID.Text, txtCODE.Text))
            {
                sErr = ctr.Update(ansInf);
                if (sErr != "")
                    lbErr.Text = sErr;
                else lbErr.Text = "Update successfull";
            }
            else
            {
                txtID.Text = ctr.Add(ansInf, ref sErr).ToString();
                if (sErr != "")
                    lbErr.Text = sErr;
                else lbErr.Text = "Insert successfull";
            }

            dgvList.Rebind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    string cate = item.OwnerTableView.DataKeyValues[item.ItemIndex]["Category"].ToString();
                    string code = item.OwnerTableView.DataKeyValues[item.ItemIndex]["Code"].ToString();
                    BUS.LookupsControl ctr = new BUS.LookupsControl();
                    sErr = ctr.Delete(cate, code);
                }
            }
            lbErr.Text = "Deleted !!!!";
            dgvList.Rebind();
        }

        private void RefreshForm(string val)
        {
            txtCODE.Text = val;
            txtNAME.Text = val;
            txtALT.Text = val;
            txtStatus.Text = val;

            lbErr.Text = val;
        }

        private DTO.LookupsInfo GetDataFromForm(DTO.LookupsInfo ansInf)
        {
            if (upPicture.UploadedFiles.Count > 0)
            {
                img.ImageUrl = upPicture.TargetFolder + "/" + Path.GetFileName(upPicture.UploadedFiles[0].FileName);
            }
            if (img.ImageUrl != "")
            {
                ansInf.Description1 = Path.GetFileName(img.ImageUrl);
            }
            ansInf.Code = txtCODE.Text;
            ansInf.Category = txtID.Text;
            ansInf.Description = txtNAME.Text;
            ansInf.Altermate = txtALT.Text;
            ansInf.Status = txtStatus.Text;

            return ansInf;
        }

        private void SetDataToForm(DTO.LookupsInfo ansInf)
        {
            string strUploadFolder = "~/" + ConfigurationManager.AppSettings["UploadFolder"].ToString() + "Images/";
            txtCODE.Text = ansInf.Code;
            txtNAME.Text = ansInf.Description;
            txtALT.Text = ansInf.Altermate;
            txtStatus.Text = ansInf.Status;
            this.img.ImageUrl = strUploadFolder + (ansInf.Description1 == "" ? "noimages.jpg" : ansInf.Description1);
        }

        protected void dgvList_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string cate = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Category"].ToString();
                string code = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Code"].ToString();
                BUS.LookupsControl ctr = new BUS.LookupsControl();
                SetDataToForm(ctr.Get(cate, code, ref sErr));
                lbErr.Text = "";
            }

        }

        protected void dgvList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            BUS.LookupsControl ctr = new BUS.LookupsControl();
            dgvList.DataSource = ctr.GetAll(Request["lookupcate"].ToString(), ref sErr);
        }
    }
}
